package com.quasar.hpatient.widget;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.change360.calendarview.CalendarDay;
import com.quasar.hpatient.R;
import com.quasar.hpatient.onInterface.OnDateChangedEvent;
import com.rey.material.app.DatePickerDialog;
import com.rey.material.app.DialogFragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class WidgetDateAdd extends LinearLayout implements View.OnClickListener {
    public WidgetDateAdd(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_date_add, this);
        select_date = (ImageView) findViewById(R.id.select_date);
        select_date.setOnClickListener(this);
        date = (TextView) findViewById(R.id.date);

        selectCalendar = Calendar.getInstance();
        date.setText(format.format(selectCalendar.getTime()));
    }
    public void setDate(CalendarDay calendarDay){
        selectCalendar=calendarDay.getCalendar();
        date.setText(format.format(selectCalendar.getTime()));
    }

    public void setTimeDate(String dateval){
        date.setText(dateval);
    }

    public WidgetDateAdd(Context context) {
        super(context);
        init(context);
    }

    //获取时间
    public String getDataTime(){
        return date.getText().toString().trim();
    }

    Calendar selectCalendar;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    ImageView select_date;
    TextView date;

    @Override
    public void onClick(View v) {

        DatePickerDialog.Builder builder = null;
        builder = new DatePickerDialog.Builder() {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
                Calendar temp = dialog.getCalendar();
                date.setText(format.format(selectCalendar.getTime()));
                if (onDateChangedEvent != null) {
                    if (!selectCalendar.equals(temp)){
                        onDateChangedEvent.onDateChanged(new CalendarDay(temp));
                    }
                }
                selectCalendar = temp;
                date.setText(format.format(selectCalendar.getTime()));
                super.onPositiveActionClicked(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
            }
        };
        Long  min=System.currentTimeMillis()-1000*60*60*24*365*10l;
        Long  max=System.currentTimeMillis();
        builder.dateRange(min,max);
        builder.date(selectCalendar.getTimeInMillis());
        builder.positiveAction("确认")
                .negativeAction("取消");

        if (getContext() instanceof AppCompatActivity) {
            AppCompatActivity activity = (AppCompatActivity) getContext();
            DialogFragment fragment = DialogFragment.newInstance(builder);
            fragment.show(activity.getSupportFragmentManager(), null);
        }

    }

    private OnDateChangedEvent onDateChangedEvent;

    public OnDateChangedEvent getOnDateChangedEvent() {
        return onDateChangedEvent;
    }

    public void setOnDateChangedEvent(OnDateChangedEvent onDateChangedEvent) {
        this.onDateChangedEvent = onDateChangedEvent;
    }

    public void hideViewDate(){
        select_date.setVisibility(View.INVISIBLE);
    }
}
